/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.server.timerTask;

import cn.ac.iie.di.ban.ycl.wx.audio.server.common.RuntimeEnv;
import static cn.ac.iie.di.ban.ycl.wx.audio.server.common.RuntimeEnv.responseMap;
import cn.ac.iie.di.ban.ycl.wx.audio.server.handler.AudioHandler;
import cn.ac.iie.di.ban.ycl.wx.audio.server.utils.Suffixutil;
import java.io.File;
import java.util.TimerTask;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class MapTask extends TimerTask {

    static Logger logger = Logger.getLogger(MapTask.class.getName());

    public MapTask() {
    }

    @Override
    public void run() {
        String responseFileDir = (String) RuntimeEnv.getParam(RuntimeEnv.responseDir);
        File responseFiles = new File(responseFileDir);
        if (responseFiles.list().length > 0) {
            for (String file : responseFiles.list()) {
//                logger.info(file);
                String fileName = file.split(Suffixutil.suffix(file))[0];
                if (!responseMap.containsKey(fileName)) {
                    //在龙存中有该图片，说明之前删除不成功，继续删除并不再加入map
                    File loongFile = new File(RuntimeEnv.getParam(RuntimeEnv.loongstore) + file);
                    if (!loongFile.exists()) {
                        responseMap.putIfAbsent(fileName, file);
                        logger.info("key = " + fileName + ",value = " + file);
                    } else {
                        logger.info(fileName + " delete from response dir " + new File(RuntimeEnv.getParam(RuntimeEnv.responseDir) + file).delete());
                    }
                }
            }
        }
        File loongstoreFiles = new File((String) RuntimeEnv.getParam(RuntimeEnv.loongstore));
        if (loongstoreFiles.list().length > 0) {
            for (String file : loongstoreFiles.list()) {
                if (file.endsWith(".json")) {
                    File loongFile = new File(RuntimeEnv.getParam(RuntimeEnv.loongstore) + file);
                    logger.info(loongFile.getAbsolutePath() + " delete " + loongFile.delete());
                }
            }
        }
    }

}
